function [] = write_minc_qmt_set(qmtVol, mincTemplate, outputBaseName, scaleFactor)
%WRITE_MINC_QMT_SET Saves a qMT set of volumes as a minc file using niak.
%
%   qmtVol: qMT set of volumes - the last dimension must be the parameter
%   dimensions.
%   mincTemplate: String containing the minc file template filename.
%   outputBaseName: String of the output minc file basename. Output files
%   will be named 'outputBaseName_qmtIndex.mnc'
%   scaleFactor: Scaling factor to push the voxel value range in the region
%   needed(e.g. some versions of Register don't handle well values between
%   0 and 1, at the authors great annoyance.
%
%   Author: Mathieu Boudreau
%   Date: February 21st 2014
%   Institute: Montreal Neurological Institute
%   
%   **Use at your own risk**

numQmt = size(qmtVol,length(size(qmtVol)));

[hdr_qmt,~]=niak_read_minc(mincTemplate);

if length(size(qmtVol))==3
    
    % The rotations and flip of the volume are needed for raw files, may
    % not be correct for other ways the qMT is loaded. Keep an eye on it.
    warning('Volume may not be in correct orientation, double check')
    
    for qmtIndex = 1:numQmt
        hdr_qmt.file_name=[outputBaseName, '_', num2str(qmtIndex), '.mnc' ];
        niak_write_minc_ss(hdr_qmt,scaleFactor*fliplr(imrotate(squeeze(qmtVol(:,:,qmtIndex)),90)));
    end
    
elseif length(size(qmtVol))==4
    
    error('3D qMT volumes not yet supported, must make check of volume mis-rotations before copying and pasting the code for 2D')

else
    
    error('qMT volume dimensions not supported')

end

end

